package com.beeantec.jnrerc.modules.course.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beeantec.jnrerc.dto.response.GroupMemberRespDTO;
import com.beeantec.jnrerc.entity.CourseChapterPeriod;
import com.beeantec.jnrerc.entity.CourseMemberStudyLog;
import com.beeantec.jnrerc.entity.GroupMember;
import com.beeantec.jnrerc.entity.Member;
import com.beeantec.jnrerc.mapper.GroupMemberMapper;
import com.beeantec.jnrerc.modules.course.service.CourseChapterPeriodService;
import com.beeantec.jnrerc.modules.course.service.CourseMemberStudyLogService;
import com.beeantec.jnrerc.modules.course.service.GroupMemberService;
import com.beeantec.jnrerc.modules.member.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class GroupMemberServiceImpl extends ServiceImpl<GroupMemberMapper, GroupMember> implements GroupMemberService {
    @Autowired
    MemberService memberService;
    @Autowired
    CourseMemberStudyLogService courseMemberStudyLogService;

    @Autowired
    CourseChapterPeriodService courseChapterPeriodService;

    @Override
    public List<GroupMemberRespDTO> getGroupMemberList(Integer groupId,String courseId) {
        List<GroupMemberRespDTO> groupMemberRespDTOList = new ArrayList<>();
        QueryWrapper<GroupMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(GroupMember::getGroupId,groupId).eq(GroupMember::getCourseId,courseId);
       List<GroupMember> groupMemberList=  baseMapper.selectList(queryWrapper);
       if (groupMemberList.size()==0){
           return groupMemberRespDTOList;
       }

       for (GroupMember groupMember:groupMemberList){
           GroupMemberRespDTO groupMemberRespDTO = new GroupMemberRespDTO();
           Member member = memberService.getById(groupMember.getMemberId());
           //获取用户已学节数
           QueryWrapper<CourseMemberStudyLog> logQueryWrapper = new QueryWrapper<>();
           logQueryWrapper.lambda().eq(CourseMemberStudyLog::getMemberId,groupMember.getMemberId())
           .eq(CourseMemberStudyLog::getCourseId,Long.parseLong(groupMember.getCourseId()));
           //计算课程总节数
           QueryWrapper<CourseChapterPeriod> totalQueryWrapper = new QueryWrapper<>();
           totalQueryWrapper.lambda().eq(CourseChapterPeriod::getCourseId,Long.parseLong(groupMember.getCourseId()));
          int studyCount = courseMemberStudyLogService.getBaseMapper().selectList(logQueryWrapper).size();
          int periodCount = courseChapterPeriodService.list(totalQueryWrapper).size();
          double progress = (double) studyCount/periodCount;
          BeanUtils.copyProperties(groupMember,groupMemberRespDTO);
          groupMemberRespDTO.setAvatarUrl(member.getAvatarUrl());
          groupMemberRespDTO.setStudyProgress(progress);
          groupMemberRespDTO.setNickName(member.getNickName());
          groupMemberRespDTOList.add(groupMemberRespDTO);
       }
        return groupMemberRespDTOList;
    }
}
